package com.m4f.business.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.springframework.security.core.GrantedAuthority;

import com.google.appengine.api.datastore.Blob;
import com.m4f.cityclient.api.models.UserCity;
import com.m4f.cityclient.common.model.M4FOAuthUser;

@SuppressWarnings("serial")
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class InternalUser implements Serializable, M4FOAuthUser {
	
	public InternalUser() {}
	
	public InternalUser(UserCity user) {
		this.id = user.getId();
		this.email = user.getEmail();
		this.name = user.getName();
		this.tests = user.getTests();
	}
	
	@PrimaryKey  
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	
	@Persistent
	//message = "{email.blank}"
	@NotNull(message="Mail blank")
	@Pattern(regexp = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$", message="Bad mail")
	private String email;
	
	private String password;
	
	@Persistent
	@NotNull(message="Name blank")
	private String name; 
	
	@Persistent
	@NotNull(message="Yes or Not is admin")
	private Boolean admin;
		
	@Persistent(serialized="true", defaultFetchGroup="true") 
	private HashMap<String, Blob> oauthAccessTokens = new HashMap<String, Blob>();
	
	@NotPersistent
	private Collection<GrantedAuthority> authorities;
	
	@Persistent
	private List<Integer> tests;
	
	public void setId(Long id) {
		this.id = id;
	}
	
	public Long getId() {
		return id;
	}
	
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}

	public Boolean getAdmin() {
		return admin;
	}
	
	public Boolean isAdmin() {
		return admin;
	}
	public void setAdmin(Boolean admin) {
		this.admin = admin;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
	
	public Collection<GrantedAuthority> getAuthorities() {
		return this.authorities;
	}
	
	public void setAuthorities(Collection<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	public String getUsername() {
		return this.email;
	}

	public boolean isAccountNonExpired() {
		return true;
	}

	public boolean isAccountNonLocked() {
		return true;
	}

	public boolean isCredentialsNonExpired() {
		return true;
	}

	public boolean isEnabled() {
		return true;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<Integer> getTests() {
		return tests;
	}

	public void setTests(List<Integer> tests) {
		this.tests = tests;
	}

	@Override
	public void setOauthAccessTokens(Map<String, Blob> tokens) {
		this.oauthAccessTokens = new HashMap<String, Blob>(tokens);		
	}

	@Override
	public Map<String, Blob> getOauthAccessTokens() {
		return this.oauthAccessTokens;
	}
}